package bean;

//import com.sun.org.apache.xml.internal.serializer.utils.SystemIDResolver;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Puspaningtyas
 */
public class DataBaseConnection {
// Sesuaikan properti jdbcURL dengan basis data anda
    private String jdbcURL = "jdbc:mysql://172.23.9.18/hewan_invertebrata";
    //private String jdbcURL = "jdbc:mysql://localhost/hewan_invertebrata";
    private String user = "root";
    private String password = "root";
//    private String jdbcURL = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
//    private String user = "MHS085314033";
//    private String password = "MHS085314033";
    private Connection connection = null;

    /** Creates a new instance of UserConnection */
    public DataBaseConnection() {
        try {
//            Class.forName("oracle.jdbc.driver.OracleDriver");
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(jdbcURL, user, password);
            System.out.println("koneksi berhasil");
        } catch (Exception ex) {
            System.out.println("message: " + ex.getMessage());
        }
    }

    public boolean isConnected() {
        if (connection != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean getClosed() {
        if (isConnected()) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return true;
        } else {
            return false;
        }
    }

    public String getJdbcURL() {
        return jdbcURL;
    }

    public void setJdbcURL(String jdbcURL) {
        this.jdbcURL = jdbcURL;
    }

    public String getUser() {
//        try {
//            String query = "select user from id_user";
//            Statement statement = (Statement) connection.createStatement();
//            ResultSet result = statement.executeQuery(query);
//            while (result.next()) {
//                user = result.getString("user");
//            }
//
//        } catch (SQLException ex) {
//            Logger.getLogger(DataBaseConnection.class.getName()).log(Level.SEVERE, null, ex);
//        }
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
//        try {
//            String query = "select password from id_user";
//            Statement statement = (Statement) connection.createStatement();
//            ResultSet result = statement.executeQuery(query);
//            while (result.next()) {
//                password = result.getString("password");
//            }
//
//        } catch (SQLException ex) {
//            Logger.getLogger(DataBaseConnection.class.getName()).log(Level.SEVERE, null, ex);
//        }
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConn(Connection connection) {
        this.connection = connection;
    }
//    public static void main(String args[]){
//        DataBaseConnection tst = new DataBaseConnection();
//        //System.out.println(tst.getUser()+"\n");
//        //System.out.println(tst.getPassword());
//    }
}


